package com.nxmu.hire.controller;

import com.nxmu.hire.common.exception.ServiceException;
import com.nxmu.hire.common.util.ResponseResult;
import com.nxmu.hire.model.EnumMap.EnumMap;
import com.nxmu.hire.model.User;
import com.nxmu.hire.model.Workload;
import com.nxmu.hire.model.param.ParameterFactory;
import com.nxmu.hire.service.UserService;
import com.nxmu.hire.service.WorkloadService;
import com.nxmu.hire.vo.AddWorkloadReqVo;
import com.nxmu.hire.vo.FindAllWorkload;
import com.nxmu.hire.vo.ModifyWorkload;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@CrossOrigin(origins = "*",allowCredentials = "true")
@RestController
@RequestMapping("/workload")
public class WorkloadController {

    private final static Logger LOGGER = LoggerFactory.getLogger(WorkloadController.class);

    @Autowired
    private WorkloadService workloadService;

    @Autowired
    private UserService userService;

    @Autowired
    ParameterFactory parameterFactory;

    @ApiOperation(value = "添加工作量")
    @PostMapping(value = "/addWorkload")
    public ResponseResult addWorkload(@Valid @RequestBody AddWorkloadReqVo addWorkloadReqVo, HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        User user = userService.findUserByUserId(addWorkloadReqVo.getUserId());
        Workload workload = new Workload();
        BeanUtils.copyProperties(addWorkloadReqVo,workload);
        workload.setLastUpdateTime(new Date());
        workload.setCreateTime(new Date());
        workload.setCollegeId(user.getCollegeId());
        workload.setCollegeName(EnumMap.collegeEnumMap.get(user.getCollegeId()));
        try {
            workloadService.save(workload);
        } catch (ServiceException e) {
            return ResponseResult.fail(e.getMessage());
        }
        return ResponseResult.success();
    }

    @ApiOperation(value = "查询所有工作量信息")
    @PostMapping(value = "/findAllWorkload")
    public ResponseResult findAllWorkload(@Valid @RequestBody FindAllWorkload findAllWorkload,HttpServletRequest request){
//        User user = (User) request.getSession().getAttribute(UserConstant.SESSION_USER);
        User user = userService.findUserByUserId(findAllWorkload.getUserId());
        List<Workload> workloads = workloadService.findAllWorkload(user.getCollegeId(),findAllWorkload);
        return ResponseResult.success(workloads);
    }

    @ApiOperation(value = "修改工作量")
    @PostMapping(value = "/modifyWorkload")
    public ResponseResult modifyWorkload(@Valid @RequestBody ModifyWorkload modifyWorkload){
        try {
            workloadService.modifyWorkload(modifyWorkload.getWorkloadId(),modifyWorkload.getWorkloadQuantity());
        } catch (ServiceException e) {
            ResponseResult.fail(e.getMessage());
        }
        return ResponseResult.success();
    }

}
